package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;

public class StockVolumeBean {
 
    public StringBuffer getVolume (String tick) 
    {
	StringBuffer sb = new StringBuffer();
	try {
	    DecimalFormat oned = new DecimalFormat("0");
	    DecimalFormat twod = new DecimalFormat("0.00");

	    String address ="http://table.finance.yahoo.com/table.csv?s="+tick+"&d="+
		MyDateAndTime.getCurrentMonth()+"&e="+MyDateAndTime.getCurrentDay()+"&f="+MyDateAndTime.getCurrentYear()+
		"&g=d&a="+MyDateAndTime.getCurrentMonth()+"&b="+MyDateAndTime.getCurrentDay()+"&c="+
		(MyDateAndTime.getCurrentYearInt()-1)+"&ignore=.csv";
	    
	    StockVolumeHistory svh = new StockVolumeHistory();
	    ArrayList a = svh.loadData(address);
	    svh = null;
	    if(a == null || a.size()==0) {
		sb.append("<BR><B>Loading data error.</B><BR><BR>");
		sb.append(MyHtml.makeLinkButton("Back", "javascript:history.go(-1);"));
		System.out.println(address);
		return sb;
	    }

	    Iterator it = a.iterator();
	    StringTokenizer stk  = null;
	    String date  = null;
	    double open  = 0.00;
	    double high  = 0.00;
	    double low   = 0.00;
	    double close = 0.00;
	    long  volume = 0;
	    long  total  = 0;
	    double average = 0.0;
	    Vector vrs = new Vector();
	    Vector vr = null;
	    String temp = null;
	    int m =0;
	    while (it.hasNext()) {
		vr = new Vector();
		stk = new StringTokenizer((String)it.next(), ",");
		while(stk.hasMoreTokens()) {
		    temp = stk.nextToken();
		    vr.add(temp);
		}
		if(vr.size()==7) {
		    vrs.add(vr);
		}
		else {
		    System.out.println(vr);
		}
	    }

	    Enumeration enum = vrs.elements();
	    while (enum.hasMoreElements()) {
		vr = (Vector)enum.nextElement();
		temp = (String)vr.elementAt(5);
		volume = Long.parseLong(temp);
		total = total+volume;
	    }
	    System.out.println("vrs.size="+vrs.size());
	    average = total/vrs.size();

	    sb.append("<TABLE width='700'>");
	    sb.append("<TR>");
	    sb.append("<TD align=center colspan='9'>");
	    sb.append(MyHtml.makeLinkButton("Back","javaScript:history.go(-1);"));
	    sb.append("</TD>");
	    sb.append("</TR>");

	    sb.append("<TR>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>DATE</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>OPEN</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>HIGH</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>LOW</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>CLOSE</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>VOLUME</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>AVERAGE V.["+oned.format(average)+"]</TD>");
	    sb.append("<TD colspan=2 align=center style='BORDER: 1px solid #669999;'>CHANGE%</TD>");
	    sb.append("<TD align=center style='BORDER: 1px solid #669999;'>C/L%</TD>");
	    sb.append("</TR>");

	    String change = null;

	    enum = vrs.elements();
	    while (enum.hasMoreElements()) {
		vr = new Vector();
		sb.append("<TR>");
		vr = (Vector)enum.nextElement();
		date   = (String)vr.elementAt(0);
		open   = Double.parseDouble((String)vr.elementAt(1));
		high   = Double.parseDouble((String)vr.elementAt(2));
		low    = Double.parseDouble((String)vr.elementAt(3));
		close  = Double.parseDouble((String)vr.elementAt(4));
		volume = Long.parseLong((String)vr.elementAt(5));
	       
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+date+"</TD>");
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+open+"</TD>");
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+high+"</TD>");
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+low+"</TD>");
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+close+"</TD>");
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+volume+"</TD>");
		sb.append("<TD align=left style='BORDER: 1px solid #669999;'>");
		
		double w = 10*volume/(average+0.0001);
		if(w >200) {
		    w =200.0;
		}
		sb.append("<img src=/images/green_line.gif width="+oned.format(w)+" height=10></TD>");
		
		change = twod.format(100*(close-open)/close);

		if(change.startsWith("-")) {
		    sb.append("<TD style='BORDER: 1px solid #669999;'><img src=/images/down.gif></TD>");
		    sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+change+"%</TD>");
		}
		else {
		    if(!change.equals("0.00")) {
			sb.append("<TD style='BORDER: 1px solid #669999;'><img src=/images/up.gif></TD>");
			sb.append("<TD align=right style='BORDER: 1px solid #669999;'>+"+change+"%</TD>");
		    }
		    else {
			sb.append("<TD style='BORDER: 1px solid #669999;'><img src=/images/unchange.gif></TD>");
			sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+change+"%</TD>");
		    }
		}
		sb.append("<TD align=right style='BORDER: 1px solid #669999;'>"+twod.format(100*(close-low)/low)+"%</TD>");
		sb.append("</TR>");
	    }

	    sb.append("</TABLE>");
	}
	catch(Exception e) {
	    System.out.println("StockVolumeBean:getVolume:"+e);
	}
	return sb;
    }
}
